package gui.JPanels;

import gui.GLJPanels.GLJPanelCurvas;
import gui.GLJPanels.GLJPanelReceptaculo;
import gui.GLListeners.GLListener;
import gui.GLListeners.GLReceptaculos.GLListenerReceptaculo;
import gui.GLListeners.GLReceptaculos.GLListenerReceptaculoCircules;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JColorChooser;

import aplicacion.Vista;
import aplicacion.frames.GaleriaReceptaculos;


public class JPanelReceptaculo extends JPanelCurvas implements Observer{

	private static final long serialVersionUID = 1L;

	public JPanelReceptaculo(GLListener GLL, GLJPanelCurvas canvas) {
		super();
		this.GLL=(GLListenerReceptaculo)GLL;
		this.canvas=(GLJPanelReceptaculo)canvas;
		this.initComponents();
	}

	protected void initComponents() {
		super.initComponents();
		
		JButton button_perfil = new JButton("Perfil");
		button_perfil.addActionListener(new Bt_perfil_Listener());
        this.add(button_perfil,BorderLayout.CENTER);	
        
//        JButton button_save = new JButton("Save");
//        button_save.addActionListener(new Bt_save_Listener());
//        this.add(button_save,BorderLayout.WEST);
        
        JButton button_color = new JButton("Elegir color");
        button_color.addActionListener(new Bt_color_Listener());
        this.add(button_color,BorderLayout.WEST);
        
        JButton button_reset = new JButton("Reset");
        button_reset.addActionListener(new Bt_reset_Listener());
        this.add(button_reset,BorderLayout.WEST);
        
        JButton button_galeria = new JButton("Galeria");
        button_galeria.addActionListener(new Bt_galeria_Listener());
        this.add(button_galeria,BorderLayout.WEST);
	}
	
	private class Bt_perfil_Listener implements ActionListener{
		public void actionPerformed(ActionEvent e){
	        canvas.getContext().makeCurrent();	
	        ((GLListenerReceptaculoCircules)GLL).revolucion();
	        canvas.repaint();
	        canvas.requestFocus();
	    }
	}
	
	private class Bt_color_Listener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			canvas.getContext().makeCurrent();
			Color newColor = JColorChooser.showDialog(null, "Elija un color", Color.GREEN);
			if (newColor != null){
				((GLListenerReceptaculo)GLL).setColor(newColor);
			}		
			canvas.repaint();
			canvas.requestFocus();
		}
	}
	
//	private class Bt_save_Listener implements ActionListener{
//		public void actionPerformed(ActionEvent e){
//			JFileChooser fc = new JFileChooser(); 
//			int returnVal = fc.showSaveDialog(null);
//            if (returnVal == JFileChooser.APPROVE_OPTION) {
//                File file = fc.getSelectedFile();            	
//                ((GLListenerReceptaculoCircules)GLL).save(file.getAbsolutePath());
//            }
//            //canvas.repaint();
//			canvas.requestFocus();
//		}
//	}
	
	private class Bt_reset_Listener implements ActionListener{
		public void actionPerformed(ActionEvent e){
	        canvas.getContext().makeCurrent();	
	        ((GLListenerReceptaculo)GLL).reset();
	        if (GLL instanceof GLListenerReceptaculoCircules){
	        	((GLListenerReceptaculoCircules)GLL).actualizaCorrespondencia();
	        }
	        canvas.repaint();
	        canvas.requestFocus();
	    }
	}
	
	private class Bt_galeria_Listener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			GaleriaReceptaculos app = new GaleriaReceptaculos(
					Vista.getInstance(),0,0,(GLListenerReceptaculoCircules)GLL,(GLJPanelReceptaculo)canvas);
	        app.setVisible(true);
		}
	}


	public void update(Observable arg0, Object arg1) {
		canvas.repaint();
        canvas.requestFocus();		
	}
}


